<template>
	<view class="">
		<u-navbar placeholder autoBack bgColor="transparent"></u-navbar>
		<view class="usertop flex align-center justify-center" :style="{background:'url(' + bgUrl + ') no-repeat'}">
			<u-image src="/static/logo.png" width="168rpx" height="188rpx"></u-image>
		</view>
		
		
		<!-- #ifdef MP -->
		  
		  <view class="flex align-center justify-center mt-4" style="background: #04BF79;height: 88rpx;width: 650rpx;margin-left: 50rpx;border-radius: 20rpx;" @click="wxlogin">
		  	<u-icon name="weixin-circle-fill" color="#fff"></u-icon>
		  	<view class="ml-1 text-white font-28">微信登录</view>
		  </view>
		<!-- #endif -->
		
		<!-- #ifndef MP -->
		<template v-if="!isloginPWD">
			<view class="mt-2 mx-4" style="height: 80rpx;">
				<u-input v-model="account" placeholder="请输入手机号" placeholderStyle="fontSize:30rpx" shape="circle"
					:customStyle="{height:'80rpx',background:'#F5F5F5'}"></u-input>
			</view>
			<view class="flex align-center justify-center mt-5 mx-4" style="height: 80rpx;">
				<u-input v-model="code" shape="circle" placeholderStyle="fontSize:30rpx" :customStyle="{height:'80rpx',background:'#F5F5F5'}"
					placeholder="请输入验证码" clearable>
					<template slot="suffix">
						<u-code ref="uCode" @change="codeChange" seconds="60" changeText="X秒重新获取"></u-code>
						<view class="font-28" style="color: #04BF79;" @click="getCode">{{tips}}</view>
					</template>
				</u-input>
			</view>
		</template>
		<template v-else>
			<view class="mt-5 mx-4" style="height: 80rpx;">
				<u-input v-model="account" placeholder="请输入手机号" placeholderStyle="fontSize:30rpx" shape="circle"
					:customStyle="{height:'80rpx',background:'#F5F5F5'}"></u-input>
			</view>
			<view class="mt-5 mx-4" style="height: 80rpx;">
				<u-input v-model="password" placeholder="请输入密码" placeholderStyle="fontSize:30rpx" shape="circle"
					:customStyle="{height:'80rpx',background:'#F5F5F5'}"></u-input>
			</view>
			<view class="mt-5 flex align-center justify-between px-4">
				<view class="font-24 text-light6" @click="$u.route('pages/login/register')">立即注册</view>
				<view class="font-24 text-light6" @click="$u.route('pages/login/changepwd')">忘记密码?</view>
			</view>
		</template>
		
		
		
		<commonButton title="登录" :background="isable?'#04BF79':'#C3FFC3'" @click="login"></commonButton>
		
		<view class="flex align-center justify-center mt-2">
			<view class="flex align-center justify-center font-28" style="color: #04BF79;" @click="changeLoginType">
				{{!isloginPWD?'密码登录':'验证码登录'}}
			</view>
		</view>
		
		
		<!-- #endif -->
		
		<view class="flex align-center justify-center" style="margin-top: 180rpx;">
			<u-image :src="isagree?'/static/select.png':'/static/noselect.png'" width="20" height="20"
				@click="isagree=!isagree"></u-image>
			<view class="ml-1 font-24" style="color:#BDBDBD;">
				我已阅读并同意<text style="color: #3289FF;" @click="$u.route('pages/login/agreement',{type:'privacy'})">《隐私政策》</text>和<text style="color: #3289FF;" @click="$u.route('pages/login/agreement',{type:'user'})">《用户协议》</text>
			</view>
		</view>
		

		




	</view>
</template>

<script>
	import {base} from '@/request/env.js'
	export default {
		data() {
			return {
				account: '',
				password: '',
				code: '',
				isloginPWD: true,
				tips: '',
				openId: '',
				isagree: false,
				bgUrl:'',
				invite_code:''
			}
		},
		computed: {
			isable() {
				if (this.isloginPWD) {
					if (!uni.$u.test.isEmpty(this.account) && !uni.$u.test.isEmpty(this.password) && this.isagree) {
						return true
					} else {
						return false
					}
				} else {
					if (!uni.$u.test.isEmpty(this.account) && !uni.$u.test.isEmpty(this.code) && this.isagree) {
						return true
					} else {
						return false
					}
				}

			}
		},
		onLoad(e) {
			this.invite_code = e.code
			// this.bgUrl = base+'/image/login.png'
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						let parmar = {
							mobile:this.account,
							event:'login'
						}
						this.$api.verification(parmar).then(res=>{
							if(res.code === 1){
								// 这里此提示会被this.start()方法中的提示覆盖
								uni.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							}
						})	
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			wxlogin(){
				if(!this.isagree){
					return uni.showToast({
						title: '请阅读并勾选协议',
						icon:'none'
					});
				}
				uni.showLoading({
					title: '登录中...',
					mask: true
				});
				uni.login({
					provider:'weixin',
					success: (reswx) => {
						console.log(reswx)
						this.$api.wxInfo({
							code:reswx.code
						}).then(res=>{
							if(res.code === 1){
								this.$api.login({openid:res.data.openid}).then(result=>{
									if(result.code === 4){
										uni.hideLoading()
										uni.$u.route('pages/login/register',{
											openid:res.data.openid,
											code:this.invite_code
										})
									}
									if (result.code == 1) {
										uni.hideLoading()
										this.$store.commit('user/setUserInfo', result.data.userinfo)
										this.$store.commit('user/setToken', result.data.userinfo.token)
										uni.showToast({
											title: result.msg,
											icon: 'none'
										});
										setTimeout(() => {
											uni.switchTab({
												url: '/pages/index/index'
											})
										}, 1000)
																		
									}
									
									
								})
							}
						})		 
					}
				})	
			},
			login() {
				if(!this.isable) return
				if(this.isloginPWD){
					let parmar = {
						mobile:this.account,
						password:this.password
					}
					this.$api.login(parmar).then(res=>{
						if(res.code === 1){
							this.$store.commit('user/setUserInfo', res.data.userinfo)
							this.$store.commit('user/setToken', res.data.userinfo.token)
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1000)
						}
					})
				}else{
					let parmar = {
						mobile:this.account,
						captcha:this.code
					}
					this.$api.mobilelogin(parmar).then(res=>{
						if(res.code === 1){
							this.$store.commit('user/setUserInfo', res.data.userinfo)
							this.$store.commit('user/setToken', res.data.userinfo.token)
							uni.showToast({
								title: res.msg,
								icon: 'none'
							});
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1000)
						}
					})
					
				}

			},
			changeLoginType() {
				this.isloginPWD = !this.isloginPWD

			}


		}
	}
</script>

<style>
	.usertop {
		background-size: 100% 272rpx;
		height: 272rpx;
	}
</style>